﻿using System;
using System.Data;
using System.Data.Odbc;
using System.Text.RegularExpressions;
using System.Data.Common;

namespace ZhhbSoft.Data
{
    /// <summary>
    ///OdbcDbProvider 的摘要说明
    /// </summary>
    public class OdbcDbProvider : DbProvider
    {
        protected OdbcDbProvider()
        {
            //怎么回事呢？
            //明白了
        }

        public OdbcDbProvider(string strConnectionString)
            : base(strConnectionString)
        {
            //no
        }

        protected override IDbConnection CreateConnection()
        {
            try
            {
                return new OdbcConnection();
            }
            catch//ok
            {
                throw new Exception("数据库连接失败！");
            }
        }

        protected override DbDataAdapter CreateDataAdapter(IDbCommand cmd)
        {
            return new OdbcDataAdapter((OdbcCommand)cmd);
        }
        //分支的修改
        protected override IDbDataParameter CreateDataParameter()
        {
            var sqlParameter = new OdbcParameter();
            return sqlParameter;
        }
        public override string SqlEncode(object value, DbType dbType)
        {//主干的修改
            switch (dbType)
            {
                case DbType.String:
                    {
                        return "'" + value.ToString().Replace("'", "''") + "'";
                    }
                case DbType.Int32:
                    {
                        return Convert.ToInt32(value).ToString();
                    }
                case DbType.Date:
                    {
                        return "#" + Convert.ToDateTime(value).ToString("MM-dd-yyyy") + "#";
                    }
                case DbType.Time:
                    {
                        return "#" + Convert.ToDateTime(value).ToString("HH:mm:ss") + "#";
                    }
                case DbType.DateTime:
                    {
                        return "#" + Convert.ToDateTime(value).ToString("MM-dd-yyyy HH:mm:ss") + "#";
                    }
                case DbType.Boolean:
                    {
                        bool boolTemp;
                        return bool.TryParse(value.ToString(), out boolTemp)
                                   ? (boolTemp ? "-1" : "0")
                                   : (value.ToString() == "0" ? "0" : "-1");
                    }
                default:
                    {
                        return "'" + value.ToString().Replace("'", "''") + "'";
                    }
            }
        }

        protected override void RebulidCommandParameter(IDbCommand command)
        {
            if (command.CommandType == CommandType.Text)
            {
                string strCommandText = command.CommandText;
                for (int i = command.Parameters.Count - 1; i >= 0; i--)
                {
                    var parameter = (IDbDataParameter)command.Parameters[i];
                    var regex = new Regex(parameter.ParameterName, RegexOptions.IgnoreCase);
                    strCommandText = regex.Replace(strCommandText, SqlEncode(parameter.Value, parameter.DbType));
                    command.Parameters.RemoveAt(parameter.ParameterName);
                }
                command.CommandText = strCommandText;
            }

        }

        public override IDbCommand CreateCommand()
        {
            return new OdbcCommand();
        }
    }
}